\(\sum{(Px(value==1)*px\_resolution ^2 )}\)

library(pixmap)
library(magrittr)
library(EBImage)
library(image.LineSegmentDetector)
library(image.ContourDetector)
library(image.CannyEdges)

setwd("~/Zellkraftwerk/packages/RJobTissueArea")

file <- file.path("inst/data","data_sum_M1730408_pos11.csv")

data.sum <- file%>%
  data.table::fread()

m.data_sum <- data.sum%>%
  as.matrix()

#cellres: pixel resolution in horizontal and vertical direction

image <-pixmapGrey(m.data_sum,cellres=c(1,1))

plot(image)

EBImage

constant threshold

grid <- expand.grid(
  sigma = seq(0.5,3,0.4),
  threshold = seq(2,4,0.2)
)
grid$min <- NA
grid$max <- NA

img2 <- image@grey * 255
# Set up plots for 96 dpi images
  #library(EBImage)
  #dm <- dim(img2)/96
  #dev.new(width = dm[1], height = dm[2])
# Low pass filter with gblur and make binary

  par(mfrow=c(1,2))
  
for(i in 1:dim(grid)[1]){

    writeLines(c(
    paste0("- sigma: ",grid$sigma[i]),
    paste0("- threshold: ", grid$threshold[i])
  ))

  #Low-pass Gaussian filter
  xb <- EBImage::gblur(img2, grid$sigma[i])
  xb <- round(xb,digits = 1)
  #plot(xb)

  image2 <- pixmapGrey(xb,cellres=c(1,1)) #(xb-min(xb))/(max(xb)-min(xb))

  plot(image2)

  grid$min[i] <- min(xb)
  grid$max[i] <- max(xb)
  
  #threshold filtering
  pos <- which(xb > grid$threshold[i])
  xt <- xb
  xt[which(xb > grid$threshold[i])] <- 1
  xt[which(xb <= grid$threshold[i])] <- 0
  
 #xt <- EBImage::thresh(xb, w=grid$w[i],
 #                      h=grid$w[i],
 #                      offset = grid$offset[i])
 #plot(xt)

 image3 <- image
 image3@grey <- xt

 plot(image3)
}
## - sigma: 0.5
## - threshold: 2

## - sigma: 0.9
## - threshold: 2

## - sigma: 1.3
## - threshold: 2

## - sigma: 1.7
## - threshold: 2

## - sigma: 2.1
## - threshold: 2

## - sigma: 2.5
## - threshold: 2

## - sigma: 2.9
## - threshold: 2

## - sigma: 0.5
## - threshold: 2.2

## - sigma: 0.9
## - threshold: 2.2

## - sigma: 1.3
## - threshold: 2.2

## - sigma: 1.7
## - threshold: 2.2

## - sigma: 2.1
## - threshold: 2.2

## - sigma: 2.5
## - threshold: 2.2

## - sigma: 2.9
## - threshold: 2.2

## - sigma: 0.5
## - threshold: 2.4

## - sigma: 0.9
## - threshold: 2.4

## - sigma: 1.3
## - threshold: 2.4

## - sigma: 1.7
## - threshold: 2.4

## - sigma: 2.1
## - threshold: 2.4

## - sigma: 2.5
## - threshold: 2.4

## - sigma: 2.9
## - threshold: 2.4

## - sigma: 0.5
## - threshold: 2.6

## - sigma: 0.9
## - threshold: 2.6

## - sigma: 1.3
## - threshold: 2.6

## - sigma: 1.7
## - threshold: 2.6

## - sigma: 2.1
## - threshold: 2.6

## - sigma: 2.5
## - threshold: 2.6

## - sigma: 2.9
## - threshold: 2.6

## - sigma: 0.5
## - threshold: 2.8

## - sigma: 0.9
## - threshold: 2.8

## - sigma: 1.3
## - threshold: 2.8

## - sigma: 1.7
## - threshold: 2.8

## - sigma: 2.1
## - threshold: 2.8

## - sigma: 2.5
## - threshold: 2.8

## - sigma: 2.9
## - threshold: 2.8

## - sigma: 0.5
## - threshold: 3

## - sigma: 0.9
## - threshold: 3

## - sigma: 1.3
## - threshold: 3

## - sigma: 1.7
## - threshold: 3

## - sigma: 2.1
## - threshold: 3

## - sigma: 2.5
## - threshold: 3

## - sigma: 2.9
## - threshold: 3

## - sigma: 0.5
## - threshold: 3.2

## - sigma: 0.9
## - threshold: 3.2

## - sigma: 1.3
## - threshold: 3.2

## - sigma: 1.7
## - threshold: 3.2

## - sigma: 2.1
## - threshold: 3.2

## - sigma: 2.5
## - threshold: 3.2

## - sigma: 2.9
## - threshold: 3.2

## - sigma: 0.5
## - threshold: 3.4

## - sigma: 0.9
## - threshold: 3.4

## - sigma: 1.3
## - threshold: 3.4

## - sigma: 1.7
## - threshold: 3.4

## - sigma: 2.1
## - threshold: 3.4

## - sigma: 2.5
## - threshold: 3.4

## - sigma: 2.9
## - threshold: 3.4

## - sigma: 0.5
## - threshold: 3.6

## - sigma: 0.9
## - threshold: 3.6

## - sigma: 1.3
## - threshold: 3.6

## - sigma: 1.7
## - threshold: 3.6

## - sigma: 2.1
## - threshold: 3.6

## - sigma: 2.5
## - threshold: 3.6

## - sigma: 2.9
## - threshold: 3.6

## - sigma: 0.5
## - threshold: 3.8

## - sigma: 0.9
## - threshold: 3.8

## - sigma: 1.3
## - threshold: 3.8

## - sigma: 1.7
## - threshold: 3.8

## - sigma: 2.1
## - threshold: 3.8

## - sigma: 2.5
## - threshold: 3.8

## - sigma: 2.9
## - threshold: 3.8

## - sigma: 0.5
## - threshold: 4

## - sigma: 0.9
## - threshold: 4

## - sigma: 1.3
## - threshold: 4

## - sigma: 1.7
## - threshold: 4

## - sigma: 2.1
## - threshold: 4

## - sigma: 2.5
## - threshold: 4

## - sigma: 2.9
## - threshold: 4

grid
##    sigma threshold min   max
## 1    0.5       2.0 0.1 203.3
## 2    0.9       2.0 0.1 200.8
## 3    1.3       2.0 0.2 194.5
## 4    1.7       2.0 0.2 185.1
## 5    2.1       2.0 0.2 174.3
## 6    2.5       2.0 0.2 162.7
## 7    2.9       2.0 0.2 151.2
## 8    0.5       2.2 0.1 203.3
## 9    0.9       2.2 0.1 200.8
## 10   1.3       2.2 0.2 194.5
## 11   1.7       2.2 0.2 185.1
## 12   2.1       2.2 0.2 174.3
## 13   2.5       2.2 0.2 162.7
## 14   2.9       2.2 0.2 151.2
## 15   0.5       2.4 0.1 203.3
## 16   0.9       2.4 0.1 200.8
## 17   1.3       2.4 0.2 194.5
## 18   1.7       2.4 0.2 185.1
## 19   2.1       2.4 0.2 174.3
## 20   2.5       2.4 0.2 162.7
## 21   2.9       2.4 0.2 151.2
## 22   0.5       2.6 0.1 203.3
## 23   0.9       2.6 0.1 200.8
## 24   1.3       2.6 0.2 194.5
## 25   1.7       2.6 0.2 185.1
## 26   2.1       2.6 0.2 174.3
## 27   2.5       2.6 0.2 162.7
## 28   2.9       2.6 0.2 151.2
## 29   0.5       2.8 0.1 203.3
## 30   0.9       2.8 0.1 200.8
## 31   1.3       2.8 0.2 194.5
## 32   1.7       2.8 0.2 185.1
## 33   2.1       2.8 0.2 174.3
## 34   2.5       2.8 0.2 162.7
## 35   2.9       2.8 0.2 151.2
## 36   0.5       3.0 0.1 203.3
## 37   0.9       3.0 0.1 200.8
## 38   1.3       3.0 0.2 194.5
## 39   1.7       3.0 0.2 185.1
## 40   2.1       3.0 0.2 174.3
## 41   2.5       3.0 0.2 162.7
## 42   2.9       3.0 0.2 151.2
## 43   0.5       3.2 0.1 203.3
## 44   0.9       3.2 0.1 200.8
## 45   1.3       3.2 0.2 194.5
## 46   1.7       3.2 0.2 185.1
## 47   2.1       3.2 0.2 174.3
## 48   2.5       3.2 0.2 162.7
## 49   2.9       3.2 0.2 151.2
## 50   0.5       3.4 0.1 203.3
## 51   0.9       3.4 0.1 200.8
## 52   1.3       3.4 0.2 194.5
## 53   1.7       3.4 0.2 185.1
## 54   2.1       3.4 0.2 174.3
## 55   2.5       3.4 0.2 162.7
## 56   2.9       3.4 0.2 151.2
## 57   0.5       3.6 0.1 203.3
## 58   0.9       3.6 0.1 200.8
## 59   1.3       3.6 0.2 194.5
## 60   1.7       3.6 0.2 185.1
## 61   2.1       3.6 0.2 174.3
## 62   2.5       3.6 0.2 162.7
## 63   2.9       3.6 0.2 151.2
## 64   0.5       3.8 0.1 203.3
## 65   0.9       3.8 0.1 200.8
## 66   1.3       3.8 0.2 194.5
## 67   1.7       3.8 0.2 185.1
## 68   2.1       3.8 0.2 174.3
## 69   2.5       3.8 0.2 162.7
## 70   2.9       3.8 0.2 151.2
## 71   0.5       4.0 0.1 203.3
## 72   0.9       4.0 0.1 200.8
## 73   1.3       4.0 0.2 194.5
## 74   1.7       4.0 0.2 185.1
## 75   2.1       4.0 0.2 174.3
## 76   2.5       4.0 0.2 162.7
## 77   2.9       4.0 0.2 151.2
write.csv(grid,"grid_resulttable.csv")

supplement

adapting threshold

grid <- expand.grid(sigma = c(1,2,3,4,5,10,100),
             offset = c(100,10,1,0,0.01,0.001,0.0001,0.00001,0.000001,0.0000001),
             w=c(1,5,10,50))

img2 <- image@grey * 255
# Set up plots for 96 dpi images
  #library(EBImage)
  #dm <- dim(img2)/96
  #dev.new(width = dm[1], height = dm[2])
# Low pass filter with gblur and make binary

for(i in 1:dim(grid)[1]){

    writeLines(c(
    paste0("- sigma: ",grid$sigma[i]),
    paste0("- offset: ", grid$offset[i]),
    paste0(" - w and h: ",grid$w[i])
  ))

  #Low-pass Gaussian filter
  xb <- EBImage::gblur(img2, grid$sigma[i])
  plot(xb)

  image2 <- pixmapGrey(xb,cellres=c(1,1)) #(xb-min(xb))/(max(xb)-min(xb))

  plot(image2)


  xt <- EBImage::thresh(xb, w=grid$w[i],
                        h=grid$w[i],
                        offset = grid$offset[i])
  plot(xt)

  image3 <- image
  image3@grey <- xt

  plot(image3)
}

image_line_segment_detector

x <- image@grey * 255

linesegments <- image_line_segment_detector(x,
                                            scale = 0.8,
                                            sigma_scale = 0.6,
                                            quant = 2,
                                            ang_th = 22.5,
                                            log_eps = 0,
                                            density_th = 0.7,
                                            n_bins = 1024,
                                            union = FALSE,
                                            union_min_length = 5,
                                            union_max_distance = 5,
                                            union_ang_th = 7,
                                            union_use_NFA = FALSE,
                                            union_log_eps = 0)

linesegments
plot(image)
plot(linesegments, add = TRUE, col = "red")

image_contour_detector

contourlines  <- image_contour_detector(x, Q = 2)
contourlines
plot(image)
plot(contourlines, add = TRUE, col = "red")

image_canny_edge_detector

edges <- image_canny_edge_detector(x)
edges
plot(edges)